<?php

namespace backend\controllers;

use common\logic\RoleLogic;
use common\models\AuthRoleModel;
use common\logic\AdminUserLogic;
use common\logic\MenuLogic;
use Yii;
use yii\helpers\ArrayHelper;
use common\models\AdminModel;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;

/**
 * AdminUserController implements the CRUD actions for AdminModel model.
 */
class AdminRoleController extends BaseController
{
    public function actionAdminRole(){
		
		$query = AuthRoleModel::find();
		$query->where(['status'=>1]);
        $query->orderBy(['id'=>SORT_DESC]);
		
    	$dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $dataProvider->sort = false;
        return $this->render('admin-role', [
            'dataProvider' => $dataProvider,
        ]);
        
    }
	
	
	public function actionCreateRole()
    {
		$post = Yii::$app->request->post();
		$model = new AuthRoleModel();
		
		if ($model->load($post) && RoleLogic::instance()->AuthRoleSave($model)) {
            return $this->redirect(['admin-role']);
        } else {
        	
//			$options = MenuLogic::instance()->getAll();
//  		$ruleArr =  ArrayHelper::toArray($options);
//  		
//  		$rulePidArr = [];
//  		foreach($ruleArr as $k=>$v){
//  			
//  			if(!isset($rulePidArr[$v['pid']])){
//  				$rulePidArr[$v['pid']] = [];
//  			}
//  			
//  			array_push($rulePidArr[$v['pid']],$v);
//  		}

			$rulePidArr = [];
			$optionsBig = MenuLogic::instance()->getAll(['pid'=>0]);
    		$ruleArrBig = ArrayHelper::toArray($optionsBig);
			foreach($ruleArrBig as $k=>$v){
				$rulePidArr[$v['id']] = ['names'=>$v['title'],'arr'=>[]];
			}
			
        	$options = MenuLogic::instance()->getAll();
    		$ruleArr =  ArrayHelper::toArray($options);
    		
    		foreach($ruleArr as $k=>$v){
    			if(!isset($rulePidArr[$v['pid']]) || $v['pid']==0){
					continue;
    			}
    			array_push($rulePidArr[$v['pid']]['arr'],$v);
    		}
			
//			var_dump($rulePidArr);
			
            return $this->render('create-role', [
                'model' => $model,
                'rules'	=> $rulePidArr
            ]);
        }
       
    }
    
    
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $post = Yii::$app->request->post();
        if($model->load($post) && $model->save()){
            return $this->redirect(['admin-role']);
        }
        else {
        	
			$rulePidArr = [];
			$optionsBig = MenuLogic::instance()->getPidAll(0);
    		$ruleArrBig = ArrayHelper::toArray($optionsBig);
			
//			var_dump($ruleArrBig);
//			exit();
			foreach($ruleArrBig as $k=>$v){
				$rulePidArr[$v['id']] = ['names'=>$v['title'],'arr'=>[]];
			}
			
        	$options = MenuLogic::instance()->getAll();
    		$ruleArr =  ArrayHelper::toArray($options);
    		
    		foreach($ruleArr as $k=>$v){
    			if(!isset($rulePidArr[$v['pid']]) || $v['pid']==0){
					continue;
    			}
    			array_push($rulePidArr[$v['pid']]['arr'],$v);
    		}
    		
//			var_dump($rulePidArr);
			
            return $this->render('create-role', [
                'model' => $model,
                'rules'	=> $rulePidArr
            ]);
        }
    }
    
	public function GetRule(){
		
		$rulePidArr = [];
		$optionsBig = MenuLogic::instance()->getPidAll(0);
		$ruleArrBig = ArrayHelper::toArray($optionsBig);
		

		foreach($ruleArrBig as $k=>$v){
			$rulePidArr[$v['id']] = ['names'=>$v['title'],'arr'=>[]];
		}
		
    	$options = MenuLogic::instance()->getAll();
		$ruleArr =  ArrayHelper::toArray($options);
		
		foreach($ruleArr as $k=>$v){
			if(!isset($rulePidArr[$v['pid']]) || $v['pid']==0){
				continue;
			}
			array_push($rulePidArr[$v['pid']]['arr'],$v);
		}
		return $rulePidArr;
	}
	
    public function actionDelete($id)
    {
    	
        RoleLogic::instance()->delete($id);

        return $this->redirect(['admin-role']);
    }
    
    /**
     * Finds the AdminModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return AdminModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = AuthRoleModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
